<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\store\model;

use app\common\model\SupplierCategory as SupplierCategoryModel;

/**
 * 供应商与分类关系模型
 * Class GoodsCategoryRel
 * @package app\store\model
 */
class SupplierCategory extends SupplierCategoryModel
{
    /**
     * 根据分类ID获取记录总数量
     * @param int $categoryId
     * @return int
     */
    public static function getCountByCategoryId(int $categoryId)
    {
        /*return (new static)->alias('m')
            ->join('goods', 'goods.goods_id = m.goods_id')
            ->where('m.category_id', '=', $categoryId)
            ->where('goods.is_delete', '=', 0)
            ->count();*/
    }

    /**
     * 获取供应商分类ID集
     * @param int $goodsId
     * @return array
     */
    public static function getCategoryIds(int $supplierId)
    {
        return (new static)->where('supplier_id', '=', $supplierId)->column('category_id');
    }

    /**
     * 批量写入供应商分类记录
     * @param int $goodsId
     * @param array $categoryIds
     * @return array|false
     */
    public static function increased(int $supplierId, array $categoryIds)
    {
        $dataset = [];
        foreach ($categoryIds as $categoryId) {
            $dataset[] = [
                'category_id' => $categoryId,
                'supplier_id' => $supplierId,
                'store_id' => self::$storeId
            ];
        }
        return (new static)->addAll($dataset);
    }

    /**
     * 更新关系记录
     * @param $goodsId
     * @param array $categoryIds 新的分类集
     * @return array|false
     * @throws \Exception
     */
    public static function updates(int $supplierId, $categoryIds)
    {
        // 已分配的分类集
        $assignCategoryIds = self::getCategoryIdsBySupplierId($supplierId);
        // 找出删除的分类
        $deleteCategoryIds = array_diff($assignCategoryIds, $categoryIds);
        if (!empty($deleteCategoryIds)) {
            static::deleteAll([
                ['supplier_id', '=', $supplierId],
                ['category_id', 'in', $deleteCategoryIds]
            ]);
        }
        // 找出添加的分类
        $newCategoryIds = array_diff($categoryIds, $assignCategoryIds);
        $dataset = [];
        foreach ($newCategoryIds as $categoryId) {
            $dataset[] = [
                'supplier_id' => $supplierId,
                'category_id' => $categoryId,
                'store_id' => self::$storeId,
            ];
        }
        return (new static)->addAll($dataset);
    }

    /**
     * 获取指定供应商的所有分类id
     * @param int $goodsId
     * @return array
     */
    public static function getCategoryIdsBySupplierId(int $goodsId)
    {
        return (new static)->where('supplier_id', '=', $goodsId)->column('category_id');
    }

    /**
     * 通过分类获取分类供应商Id
     * @param array $param
     * @return \think\db\BaseQuery
     */
    public function getSupplierIdsByCategoryIds($categoryIds)
    {
        //分类ID
        $where = [['category_id', 'in', $categoryIds]]; //对应的分类ID
        $supplier_ids = (new static)->field('supplier_id')->where($where)->select()->toArray();
        return $supplier_ids;
    }

}
